package com.study.goal.service.impl;

import cn.hutool.core.bean.BeanUtil;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.page.TableDataInfo;
import com.study.common.utils.StringUtils;
import com.study.goal.domain.MemberLevelRule;
import com.study.goal.domain.bo.MemberLevelRuleBo;
import com.study.goal.domain.vo.MemberLevelRuleVo;
import com.study.goal.mapper.MemberLevelRuleMapper;
import com.study.goal.service.IMemberLevelRuleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 会员等级规则Service业务层处理
 *
 * @author MT
 * @date 2025-09-03
 */
@RequiredArgsConstructor
@Service
public class MemberLevelRuleServiceImpl implements IMemberLevelRuleService {

    private final MemberLevelRuleMapper baseMapper;

    /**
     * 查询会员等级规则
     */
    @Override
    public MemberLevelRuleVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询会员等级规则列表
     */
    @Override
    public TableDataInfo<MemberLevelRuleVo> queryPageList(MemberLevelRuleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MemberLevelRule> lqw = buildQueryWrapper(bo);
        Page<MemberLevelRuleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询会员等级规则列表
     */
    @Override
    public List<MemberLevelRuleVo> queryList(MemberLevelRuleBo bo) {
        LambdaQueryWrapper<MemberLevelRule> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MemberLevelRule> buildQueryWrapper(MemberLevelRuleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MemberLevelRule> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getLevelName()), MemberLevelRule::getLevelName, bo.getLevelName());
        lqw.eq(StringUtils.isNotBlank(bo.getLevelCode()), MemberLevelRule::getLevelCode, bo.getLevelCode());
        lqw.eq(bo.getMinIntegral() != null, MemberLevelRule::getMinIntegral, bo.getMinIntegral());
        lqw.eq(bo.getMaxIntegral() != null, MemberLevelRule::getMaxIntegral, bo.getMaxIntegral());
        lqw.eq(StringUtils.isNotBlank(bo.getBenefits()), MemberLevelRule::getBenefits, bo.getBenefits());
        lqw.eq(bo.getDiscountRate() != null, MemberLevelRule::getDiscountRate, bo.getDiscountRate());
        lqw.eq(bo.getDurationDays() != null, MemberLevelRule::getDurationDays, bo.getDurationDays());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), MemberLevelRule::getStatus, bo.getStatus());
        lqw.eq(bo.getSortOrder() != null, MemberLevelRule::getSortOrder, bo.getSortOrder());
        return lqw;
    }

    /**
     * 新增会员等级规则
     */
    @Override
    public Boolean insertByBo(MemberLevelRuleBo bo) {
        MemberLevelRule add = BeanUtil.toBean(bo, MemberLevelRule.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改会员等级规则
     */
    @Override
    public Boolean updateByBo(MemberLevelRuleBo bo) {
        MemberLevelRule update = BeanUtil.toBean(bo, MemberLevelRule.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MemberLevelRule entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除会员等级规则
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
